package com.yonyou.utils;


import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文件工具类
 *
 * @author fengjiening
 * @since 2023-02-28
 */
public class FileUtil {
    public static void openFile(  Project project) {
        FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, false , true, false, true);
        // FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false , true, false, true);
        VirtualFile[] virtualFiles = FileChooser.chooseFiles(descriptor, project, null);
        for (VirtualFile virtualFile : virtualFiles) {
            String name = virtualFile.getName();
            System.out.println("name = " + name);
            StringBuilder buffer = new StringBuilder();
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(virtualFile.getInputStream()));
                String line;
                while ((line = bufferedReader.readLine()) != null){
                    buffer.append(line);
                    buffer.append("\n");
                }
                System.out.println("buffer = " + buffer.toString());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
    public static Map<String, String> readIniFile(String filePath) {
        Map<String, String> configMap = new HashMap<>();
        File file = new File(filePath);

        if (!file.exists() || !file.isFile()) {
            System.err.println("配置文件不存在: " + filePath);
            return configMap;
        }

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 忽略空行和注释行（这里假设以#开头的行为注释）
                if (line.trim().isEmpty() || line.startsWith("#")) {
                    continue;
                }

                // 分割键和值
                String[] parts = line.split("=", 2);
                if (parts.length == 2) {
                    String key = parts[0].trim();
                    String value = parts[1].trim();

                    // 将键值对添加到Map中
                    configMap.put(key, value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return configMap;
    }


}
